package com.example.pmanagesys.controller;
// 档案管理
import com.example.pmanagesys.controller.dto.UserDto;
import com.example.pmanagesys.entity.User;
import com.example.pmanagesys.mapper.UserMapper;
import com.example.pmanagesys.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@RestController
@RequestMapping("/user") //路径
public class UserController {

    @Autowired //注入其他类
    private UserMapper userMapper;

    @Autowired //注入其他类
    private UserService userService;

    // 登录页面  http://localhost:8080/user/login
    @GetMapping("/login")
    public ModelAndView login() {
        return new ModelAndView("login");
    }

    // 员工档案页面  http://localhost:8080/user/file
    @GetMapping("/file")
    public ModelAndView staff() {
        return new ModelAndView("UserFile");
    }

    // 新增或者更新
    @PostMapping
    public Integer save(@RequestBody User user) { // @RequestBody可以将前端的JSON对象转成后台的Java对象
//        if (Objects.equals(user.getPhone(), "")) { user.setPhone("--");}
        return userService.save(user);
    }

    // 删除  根据id去删除   localhost:8080/user/81110
    @DeleteMapping("/{id}")
    public Integer delete(@PathVariable Integer id) {  // {id}与@PathVariable的id对应
        return userMapper.deleteById(id);
    }

    // 批量删除  localhost:8080/user/del/batch   发送json对象 [81106, 81110]
    @PostMapping("/del/batch")
    public Integer deleteBatch(@RequestBody List<Integer> ids) {// @RequestBody可以将前端的JSON对象转成后台的Java对象
        return userMapper.deleteBatchByIds(ids);
    }

    //  分页查询  http://localhost:8080/user/page?pageNum=1&pageSize=10&z_name=&id=&address=&create_time=&username=
    @GetMapping("/page")
    public Map<String, Object> findPage(@RequestParam Integer pageNum, @RequestParam Integer pageSize ,
                                        @RequestParam String z_name, @RequestParam String id ,
                                        @RequestParam String address, @RequestParam String create_time,
                                        @RequestParam String username) {
        // 模糊查询的参数 (可以为空）
        z_name = "%"+z_name+"%";
        id = "%"+id+"%";
        address = "%"+address+"%";
        create_time = "%"+create_time+"%";
        username = "%"+username+"%";
        // 某一页的数据
        pageNum = (pageNum - 1) * pageSize  ;
        List<User> data = userMapper.selectPage(pageNum, pageSize,z_name,id,address,create_time,username);
        // 总条数
        Integer total = userMapper.selectTotal(z_name,id,address,create_time,username) ;
        // 封装一个map装数据
        Map<String, Object> res = new HashMap<>();
        res.put("Userdata", data);
        res.put("total",total);
        return res;
    }

    // 登录
    @PostMapping("/login")
    public boolean login(@RequestBody UserDto userDTO , HttpSession session) { // @RequestBody可以将前端的JSON对象转成后台的Java对象
        if (StringUtils.isEmpty(userDTO.getUsername()) || StringUtils.isEmpty(userDTO.getPassword())){ // 后端再次验证是否为空
            return false;
        }
        return userService.login(userDTO,session);
    }

    // 退出登录,  返回成功的HTTP状态码200
    @GetMapping("/logout")
    public ResponseEntity<Void> logout(HttpSession session) {
        session.removeAttribute("user"); // 移除用户
        return ResponseEntity.ok().build();
    }

    //  查询当前用户资料 localhost:8080/user/now?username=
    @GetMapping("/now")
    public Map<String, Object>  findNow(@RequestParam String username) {
        Map<String, Object> res = new HashMap<>();
        List<User> data = userMapper.findNow(username);
        res.put("UserNowdata", data);
        res.put("total",1);
        return res;
    }

    //  查询所有员工名字   http://localhost:8080/user/name
    @GetMapping("/name")
    public Map<String, Object>  findUserName() {
        Map<String, Object> res = new HashMap<>();
        List<String> data = userMapper.findUserName();
        res.put("nameData", data);
        return res;
    }


}
